home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / GFSTEST.PY < prev    next >
Encoding:
Text File  |  1999-07-28  |  7.0 KB  |  225 lines

  1.  
  2. """test script for gadfly client and server
  3.  
  4. Usage:  This script interacts with the test database generated
  5.   by gftest.py.  To start the server from the directory containing
  6.   the dbtest directory use:
  7.  
  8.      python gfstest.py start
  9.  
  10.   THIS WILL ONLY WORK IF YOU CREATED THE test DATABASE IN
  11.   DIRECTORY dbtest FIRST USING 
  12.  
  13.      python gftest.py dbtest
  14.  
  15.   UNLESS YOU RUN THE SERVER IN THE BACKGROUND THE SERVER WILL
  16.   HOG THE WINDOW YOU STARTED IT IN AND YOU WILL HAVE TO USE ANOTHER
  17.   WINDOW UNTIL THE SERVER IS SHUT DOWN (SEE BELOW).
  18.  
  19.   Then from *anywhere* (on the same machine) access the database
  20.   using
  21.      python gfstest.py restart
  22.        - restart the server (reread the database)
  23.      python gfstest.py checkpoint
  24.        - force checkpoint the server
  25.      python gfstest.py queries
  26.        - run some example queries and updates
  27.      python gfstest.py policy_test
  28.        - test the policies test and test1 created by the startup
  29.          function in this module.
  30.      python gfstest.py bogusshutdown
  31.        - attempt to shut down the server with a bogus password
  32.          [should generate an exception]
  33.  
  34. ...and finally
  35.      python gfstest.py shutdown
  36.        - shut down the server for real.
  37.  
  38. As mentioned the startup function of this module illustrates
  39. how to create a "startup" function for a server and initialize policy
  40. objects with named, prepared queries.
  41.  
  42. """
  43.  
  44. PORT = 2222
  45. DB = "test"
  46. DBDIR = "dbtest"
  47. PW = "admin"
  48. STARTUP = "gfstest"
  49.  
  50. import sys, socket
  51.  
  52.  
  53. def main():
  54.     argv = sys.argv
  55.     command = argv[1]
  56.     if command=="start":
  57.         print "attempting to start the server"
  58.         from gfserve import Server
  59.         print "making a server on", PORT, DB, DBDIR, PW, STARTUP
  60.         S = Server(PORT, DB, DBDIR, PW, STARTUP)
  61.         print "initializing the server"
  62.         S.init()
  63.         print "starting the server", S.connection
  64.         S.start()
  65.     elif command=="shutdown":
  66.         dosimple("shutdown", PW)
  67.     elif command=="bogusshutdown":
  68.         print "BOGUS shutdown attempt"
  69.         dosimple("shutdown", "bad password")
  70.     elif command=="restart":
  71.         dosimple("restart", PW)
  72.     elif command=="checkpoint":
  73.         dosimple("checkpoint", PW)
  74.     elif command=="queries":
  75.         doqueries()
  76.     elif command=="policy_test":
  77.         policy_test()
  78.     else:
  79.         print "unknown command", command
  80.         print __doc__
  81.  
  82. def policy_test():
  83.     """test the test1 and test policies"""
  84.     print "testing non-admin policies test and test1"
  85.     from gfclient import gfclient
  86.     import sys
  87.     machine = socket.gethostname()
  88.     conn = gfclient("test", PORT, "test", machine)
  89.     cursor = conn.cursor()
  90.     print "testing test policy: nan values before:"
  91.     cursor.execute_prepared("getnan")
  92.     for x in cursor.fetchall():
  93.         print x
  94.     print "updating nan"
  95.     cursor.execute_prepared("updatenan", ("pabst", 4))
  96.     print "nan after"
  97.     cursor.execute_prepared("getnan")
  98.     for x in cursor.fetchall():
  99.         print x
  100.     print "updating nan again"
  101.     cursor.execute_prepared("updatenan", ("rollingrock", 1))
  102.     print "trying an illegal update"
  103.     try:
  104.         cursor.execute("delete from frequents")
  105.     except:
  106.         print "exception", sys.exc_type, sys.exc_value
  107.         print "as expected"
  108.     else:
  109.         raise "DAMN!", "illegal query apparently completed!!!"
  110.     print; print "testing policy test1"; print
  111.     conn = gfclient("test1", PORT, "test1", machine)
  112.     cursor = conn.cursor()
  113.     print "getting norm"
  114.     cursor.execute_prepared("qlike", ("norm",))
  115.     print cursor.description
  116.     for x in cursor.fetchall():
  117.         print x
  118.     print "trying an illegal query again"
  119.     try:
  120.         cursor.execute("create table test(name varchar)")
  121.     except:
  122.         print "exception", sys.exc_type, sys.exc_value
  123.         print "as expected"
  124.     else:
  125.         raise "Damn!(2)", "illegal query apparently completed"
  126.  
  127. def startup(admin_policy, connection, Server_instance):
  128.     """example startup script.
  129.  
  130.        add a policies test and test1 passwords same
  131.          test1 is allowed to query the frequents table by name
  132.          test is allowed to update likes where drinker='nan'
  133.        also add prepared query dumpwork to admin_policy.
  134.     """
  135.     from gfserve import Policy
  136.     admin_policy["dumpwork"] = "select * from work"
  137.     test1 = Policy("test1", "test1", connection, queries=0)
  138.     test = Policy("test", "test", connection, queries=0)
  139.     test1["qlike"] = "select * from likes where drinker=?"
  140.     test["updatenan"] = """
  141.       update likes
  142.       set beer=?, perday=?
  143.       where drinker='nan'
  144.     """
  145.     test["getnan"] = """
  146.     select * from likes where drinker='nan'
  147.     """
  148.     return {"test": test, "test1": test1}
  149.  
  150. def doqueries():
  151.     print "executing queries and updates"
  152.     from gfclient import gfclient
  153.     import sys
  154.     machine = socket.gethostname()
  155.     conn = gfclient("admin", PORT, PW, machine)
  156.     cursor = conn.cursor()
  157.     for q in admin_queries:
  158.         print;print;print q;print
  159.         try:
  160.             cursor.execute(q)
  161.         except:
  162.             print "exception in execute"
  163.             print sys.exc_type
  164.             v = sys.exc_value
  165.             from types import TupleType, ListType
  166.             if type(v) in (TupleType, ListType):
  167.                for x in v: print x
  168.             else:
  169.                print v
  170.         else:
  171.             print "executed"
  172.             #print q
  173.             print "description"
  174.             print cursor.description
  175.             print "results"
  176.             try:
  177.                 r = cursor.fetchall()
  178.                 if r is None:
  179.                     print "no results"
  180.                 else:
  181.                     for x in r:
  182.                         print x
  183.             except:
  184.                 print "exception in results"
  185.                 print sys.exc_type, sys.exc_value
  186.                 print dir(cursor)
  187.     # try dumpwork
  188.     print; print; print "dumpwork"; print
  189.     cursor.execute_prepared("dumpwork")
  190.     for x in cursor.fetchall():
  191.         print x
  192.     # try dynamic parameters
  193.     stat = """
  194.     select distinct drinker
  195.     from likes l, serves s
  196.     where l.beer = s.beer and s.bar=?
  197.     """
  198.     print; print stat; print "dynamic query ?=cheers"
  199.     cursor.execute(stat, ("cheers",))
  200.     for x in cursor.fetchall():
  201.         print x
  202.             
  203. admin_queries = [
  204. """select count(*) from work""",
  205. """select * from frequents""",
  206. """select count(*) from frequents""",
  207. """select count(drinker) from frequents""",
  208. """insert into frequents(drinker, bar, perweek)
  209.      values ('sally', 'cheers', 2)""",
  210. """select * from frequents""",
  211. """select syntax error from work""",
  212. """select drinker, count(bar) from frequents
  213.    group by drinker""",
  214. ]
  215.  
  216. def dosimple(command, pw):
  217.     print "attempting remote %s (%s) for server on local machine" % (command, pw)
  218.     from gfclient import gfclient
  219.     machine = socket.gethostname()
  220.     conn = gfclient("admin", PORT, pw, machine)
  221.     action = getattr(conn, command)
  222.     print action()
  223.  
  224. if __name__=="__main__":
  225.     main()